import router from "@/router"
import { tabbarPaths } from "@/router/tabbar"

let uniResolve = null
let uniTimeout = null
const uniPromise = new Promise(resolve => {
  uniResolve = resolve

  uniTimeout = setTimeout(() => {
    resolve(false)
  }, 5000)
})

export function initUni() {
  document.addEventListener('UniAppJSBridgeReady', function() {
    clearTimeout(uniTimeout)
    uniResolve(uni)
  })  
}

export async function isAppEnv() {
  const uni = await uniPromise
  return new Promise(resolve => {
    uni.webView.getEnv(res => {
      resolve(res.plus)
    })
  })
}


export async function postMessage(data) {
  const uni = await uniPromise
  if (!uni) return
  uni.postMessage({
    data
  })
}

export async function notify() {
  const uni = await uniPromise
  if (!uni) return
  postMessage({
    event: 'action',
    type: 'notify'
  })
}

export async function statusBarHeight() {
  const uni = await uniPromise
  if (!uni) return
  postMessage({
    event: 'action',
    type: 'statusBarHeight'
  })
}

export async function sendIsTabbarView(isTabbarView) {
  const uni = await uniPromise
  if (!uni) return
  
  postMessage({
    event: 'data',
    data: {
      isTabbarView
    }
  })
}
